From 3c4b3f830beb8ba26b308ce276f658aed35c98af Mon Sep 17 00:00:00 2001
From: Micah N Gorrell <mgorrell@codeweavers.com>
Date: Thu, 12 Sep 2019 12:03:13 -0600
Subject: [PATCH] advapi32: Replace usage of internal SERV_ functions with
 exported functions

This is in preparation for moving the services RPC functions into
sechost.

Signed-off-by: Micah N Gorrell <mgorrell@codeweavers.com>
---
 dlls/advapi32/advapi32_misc.h |  3 ---
 dlls/advapi32/security.c      | 32 +++++++++++++++++++++++++-------
 dlls/advapi32/service.c       |  6 +++---
 3 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/dlls/advapi32/advapi32_misc.h b/dlls/advapi32/advapi32_misc.h
index 4ced1e8a648..77b56420ba3 100644
--- a/dlls/advapi32/advapi32_misc.h
+++ b/dlls/advapi32/advapi32_misc.h
@@ -32,9 +32,6 @@ BOOL ADVAPI_GetComputerSid(PSID sid) DECLSPEC_HIDDEN;
 BOOL lookup_local_wellknown_name(const LSA_UNICODE_STRING*, PSID, LPDWORD, LPWSTR, LPDWORD, PSID_NAME_USE, BOOL*) DECLSPEC_HIDDEN;
 BOOL lookup_local_user_name(const LSA_UNICODE_STRING*, PSID, LPDWORD, LPWSTR, LPDWORD, PSID_NAME_USE, BOOL*) DECLSPEC_HIDDEN;
 WCHAR *SERV_dup(const char *str) DECLSPEC_HIDDEN;
-DWORD SERV_OpenSCManagerW(LPCWSTR, LPCWSTR, DWORD, SC_HANDLE*) DECLSPEC_HIDDEN;
-DWORD SERV_OpenServiceW(SC_HANDLE, LPCWSTR, DWORD, SC_HANDLE*) DECLSPEC_HIDDEN;
-NTSTATUS SERV_QueryServiceObjectSecurity(SC_HANDLE, SECURITY_INFORMATION, PSECURITY_DESCRIPTOR, DWORD, LPDWORD) DECLSPEC_HIDDEN;
 const WCHAR *get_wellknown_privilege_name(const LUID *) DECLSPEC_HIDDEN;
 
 /* memory allocation functions */
diff --git a/dlls/advapi32/security.c b/dlls/advapi32/security.c
index a7707f15d39..74eda932826 100644
--- a/dlls/advapi32/security.c
+++ b/dlls/advapi32/security.c
@@ -416,15 +416,19 @@ static inline DWORD get_security_file( LPCWSTR full_file_name, DWORD access, HAN
 static inline DWORD get_security_service( LPWSTR full_service_name, DWORD access, HANDLE *service )
 {
     SC_HANDLE manager = 0;
-    DWORD err;
 
-    err = SERV_OpenSCManagerW( NULL, NULL, access, (SC_HANDLE *)&manager );
-    if (err == ERROR_SUCCESS)
+    manager = OpenSCManagerW( NULL, NULL, access );
+    if (manager)
     {
-        err = SERV_OpenServiceW( manager, full_service_name, access, (SC_HANDLE *)service );
+        *service = OpenServiceW( manager, full_service_name, access);
         CloseServiceHandle( manager );
+
+        if (*service)
+        {
+            return ERROR_SUCCESS;
+        }
     }
-    return err;
+    return GetLastError();
 }
 
 /* helper function for SE_REGISTRY_KEY objects in [Get|Set]NamedSecurityInfo */
@@ -1804,7 +1808,14 @@ DWORD WINAPI GetSecurityInfo(
     switch (ObjectType)
     {
     case SE_SERVICE:
-        status = SERV_QueryServiceObjectSecurity(hObject, SecurityInfo, NULL, 0, &n1);
+        if (QueryServiceObjectSecurity(hObject, SecurityInfo, NULL, 0, &n1))
+        {
+            status = STATUS_SUCCESS;
+        }
+        else
+        {
+            status = RtlGetLastNtStatus();
+        }
         break;
     default:
         status = NtQuerySecurityObject(hObject, SecurityInfo, NULL, 0, &n1);
@@ -1820,7 +1831,14 @@ DWORD WINAPI GetSecurityInfo(
     switch (ObjectType)
     {
     case SE_SERVICE:
-        status = SERV_QueryServiceObjectSecurity(hObject, SecurityInfo, sd, n1, &n2);
+        if (QueryServiceObjectSecurity(hObject, SecurityInfo, sd, n1, &n2))
+        {
+            status = STATUS_SUCCESS;
+        }
+        else
+        {
+            status = RtlGetLastNtStatus();
+        }
         break;
     default:
         status = NtQuerySecurityObject(hObject, SecurityInfo, sd, n1, &n2);
diff --git a/dlls/advapi32/service.c b/dlls/advapi32/service.c
index 231f0267e98..2781a128239 100644
--- a/dlls/advapi32/service.c
+++ b/dlls/advapi32/service.c
@@ -897,7 +897,7 @@ SC_HANDLE WINAPI OpenSCManagerA( LPCSTR lpMachineName, LPCSTR lpDatabaseName,
  *
  * See OpenSCManagerA.
  */
-DWORD SERV_OpenSCManagerW( LPCWSTR lpMachineName, LPCWSTR lpDatabaseName,
+static DWORD SERV_OpenSCManagerW( LPCWSTR lpMachineName, LPCWSTR lpDatabaseName,
                            DWORD dwDesiredAccess, SC_HANDLE *handle )
 {
     DWORD r;
@@ -1049,7 +1049,7 @@ SC_HANDLE WINAPI OpenServiceA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
  *
  * See OpenServiceA.
  */
-DWORD SERV_OpenServiceW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
+static DWORD SERV_OpenServiceW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
                          DWORD dwDesiredAccess, SC_HANDLE *handle )
 {
     DWORD err;
@@ -2484,7 +2484,7 @@ BOOL WINAPI ChangeServiceConfig2W( SC_HANDLE hService, DWORD dwInfoLevel,
     return err == ERROR_SUCCESS;
 }
 
-NTSTATUS SERV_QueryServiceObjectSecurity(SC_HANDLE hService,
+static NTSTATUS SERV_QueryServiceObjectSecurity(SC_HANDLE hService,
        SECURITY_INFORMATION dwSecurityInformation,
        PSECURITY_DESCRIPTOR lpSecurityDescriptor,
        DWORD cbBufSize, LPDWORD pcbBytesNeeded)

From 5e04c405f03519027f27e233d5fb1e2c91b010ab Mon Sep 17 00:00:00 2001
From: Micah N Gorrell <mgorrell@codeweavers.com>
Date: Thu, 12 Sep 2019 12:03:15 -0600
Subject: [PATCH] advapi32: Move RPC interfaces from advapi32 into sechost

Signed-off-by: Micah N Gorrell <mgorrell@codeweavers.com>
---
 configure                             |   2 +
 configure.ac                          |   1 +
 dlls/advapi32/Makefile.in             |   5 +-
 dlls/advapi32/advapi32.spec           |  92 +++++++++++-----------
 dlls/sechost/Makefile.in              |   8 ++
 dlls/sechost/sechost.spec             |  46 +++++++++++
 dlls/{advapi32 => sechost}/service.c  | 109 ++++++++++++++------------
 dlls/{advapi32 => sechost}/svcctl.idl |   0
 8 files changed, 164 insertions(+), 99 deletions(-)
 create mode 100644 dlls/sechost/Makefile.in
 create mode 100644 dlls/sechost/sechost.spec
 rename dlls/{advapi32 => sechost}/service.c (97%)
 rename dlls/{advapi32 => sechost}/svcctl.idl (100%)

diff --git a/configure b/configure
index eee834ef0a4..31ad9f73349 100755
--- a/configure
+++ b/configure
@@ -1538,6 +1538,7 @@ enable_schedsvc
 enable_scrobj
 enable_scrrun
 enable_scsiport_sys
+enable_sechost
 enable_secur32
 enable_security
 enable_sensapi
@@ -20701,6 +20702,7 @@ wine_fn_config_makefile dlls/scrobj enable_scrobj
 wine_fn_config_makefile dlls/scrrun enable_scrrun
 wine_fn_config_makefile dlls/scrrun/tests enable_tests
 wine_fn_config_makefile dlls/scsiport.sys enable_scsiport_sys
+wine_fn_config_makefile dlls/sechost enable_sechost
 wine_fn_config_makefile dlls/secur32 enable_secur32
 wine_fn_config_makefile dlls/secur32/tests enable_tests
 wine_fn_config_makefile dlls/security enable_security
diff --git a/configure.ac b/configure.ac
index 72b6ec7f8d6..9844cae70d7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3622,6 +3622,7 @@ WINE_CONFIG_MAKEFILE(dlls/scrobj)
 WINE_CONFIG_MAKEFILE(dlls/scrrun)
 WINE_CONFIG_MAKEFILE(dlls/scrrun/tests)
 WINE_CONFIG_MAKEFILE(dlls/scsiport.sys)
+WINE_CONFIG_MAKEFILE(dlls/sechost)
 WINE_CONFIG_MAKEFILE(dlls/secur32)
 WINE_CONFIG_MAKEFILE(dlls/secur32/tests)
 WINE_CONFIG_MAKEFILE(dlls/security)
diff --git a/dlls/advapi32/Makefile.in b/dlls/advapi32/Makefile.in
index e530adac4e8..f8f9c567e57 100644
--- a/dlls/advapi32/Makefile.in
+++ b/dlls/advapi32/Makefile.in
@@ -1,7 +1,7 @@
 EXTRADEFS = -D_ADVAPI32_
 MODULE    = advapi32.dll
 IMPORTLIB = advapi32
-IMPORTS   = kernelbase
+IMPORTS   = kernelbase sechost
 DELAYIMPORTS = rpcrt4
 EXTRALIBS = $(SECURITY_LIBS)
 
@@ -16,9 +16,6 @@ C_SRCS = \
 	lsa.c \
 	registry.c \
 	security.c \
-	service.c \
 	wmi.c
 
-IDL_SRCS = svcctl.idl
-
 RC_SRCS = version.rc
diff --git a/dlls/advapi32/advapi32.spec b/dlls/advapi32/advapi32.spec
index 3db5612c61a..ea5cb54e166 100644
--- a/dlls/advapi32/advapi32.spec
+++ b/dlls/advapi32/advapi32.spec
@@ -89,10 +89,10 @@
 @ stdcall BuildTrusteeWithSidA(ptr ptr)
 @ stdcall BuildTrusteeWithSidW(ptr ptr)
 # @ stub CancelOverlappedAccess
-@ stdcall ChangeServiceConfig2A(long long ptr)
-@ stdcall ChangeServiceConfig2W(long long ptr)
-@ stdcall ChangeServiceConfigA(long long long long wstr str ptr str str str str)
-@ stdcall ChangeServiceConfigW(long long long long wstr wstr ptr wstr wstr wstr wstr)
+@ stdcall -import ChangeServiceConfig2A(long long ptr)
+@ stdcall -import ChangeServiceConfig2W(long long ptr)
+@ stdcall -import ChangeServiceConfigA(long long long long wstr str ptr str str str str)
+@ stdcall -import ChangeServiceConfigW(long long long long wstr wstr ptr wstr wstr wstr wstr)
 # @ stub CheckForHiberboot
 @ stdcall -import CheckTokenMembership(long ptr ptr)
 @ stdcall ClearEventLogA (long str)
@@ -100,12 +100,12 @@
 # @ stub CloseCodeAuthzLevel
 @ stdcall CloseEncryptedFileRaw(ptr)
 @ stdcall CloseEventLog (long)
-@ stdcall CloseServiceHandle(long)
+@ stdcall -import CloseServiceHandle(long)
 # @ stub CloseThreadWaitChainSession
 @ stdcall CloseTrace(int64)
 @ stdcall CommandLineFromMsiDescriptor(wstr ptr ptr)
 # @ stub ComputeAccessTokenFromCodeAuthzLevel
-@ stdcall ControlService(long long ptr)
+@ stdcall -import ControlService(long long ptr)
 # @ stub ControlServiceExA
 # @ stub ControlServiceExW
 @ stdcall ControlTraceA(int64 str ptr long)
@@ -143,8 +143,8 @@
 @ stdcall CreateProcessWithLogonW(wstr wstr wstr long wstr wstr long ptr wstr ptr ptr)
 @ stdcall CreateProcessWithTokenW(long long wstr wstr long ptr wstr ptr ptr)
 @ stdcall -import CreateRestrictedToken(long long long ptr long ptr long ptr ptr)
-@ stdcall CreateServiceA(long str str long long long long str str ptr str str str)
-@ stdcall CreateServiceW(long wstr wstr long long long long wstr wstr ptr wstr wstr wstr)
+@ stdcall -import CreateServiceA(long str str long long long long str str ptr str str str)
+@ stdcall -import CreateServiceW(long wstr wstr long long long long wstr wstr ptr wstr wstr wstr)
 # @ stub CreateTraceInstanceId
 @ stdcall -import CreateWellKnownSid(long ptr ptr ptr)
 # @ stub CredBackupCredentials
@@ -235,7 +235,7 @@
 @ stdcall DecryptFileA(str long)
 @ stdcall DecryptFileW(wstr long)
 @ stdcall -import DeleteAce(ptr long)
-@ stdcall DeleteService(long)
+@ stdcall -import DeleteService(long)
 @ stdcall DeregisterEventSource(long)
 @ stdcall -import DestroyPrivateObjectSecurity(ptr)
 # @ stub DuplicateEncryptionInfoFile
@@ -270,15 +270,15 @@
 @ stdcall EncryptFileW(wstr)
 # @ stub EncryptedFileKeyInfo
 # @ stub EncryptionDisable
-@ stdcall EnumDependentServicesA(long long ptr long ptr ptr)
-@ stdcall EnumDependentServicesW(long long ptr long ptr ptr)
+@ stdcall -import EnumDependentServicesA(long long ptr long ptr ptr)
+@ stdcall -import EnumDependentServicesW(long long ptr long ptr ptr)
 @ stdcall -import EnumDynamicTimeZoneInformation(long ptr)
 @ stub EnumServiceGroupA
 @ stub EnumServiceGroupW
-@ stdcall EnumServicesStatusA (long long long ptr long ptr ptr ptr)
-@ stdcall EnumServicesStatusExA(long long long long ptr long ptr ptr ptr str)
-@ stdcall EnumServicesStatusExW(long long long long ptr long ptr ptr ptr wstr)
-@ stdcall EnumServicesStatusW (long long long ptr long ptr ptr ptr)
+@ stdcall -import EnumServicesStatusA (long long long ptr long ptr ptr ptr)
+@ stdcall -import EnumServicesStatusExA(long long long long ptr long ptr ptr ptr str)
+@ stdcall -import EnumServicesStatusExW(long long long long ptr long ptr ptr ptr wstr)
+@ stdcall -import EnumServicesStatusW (long long long ptr long ptr ptr ptr)
 @ stdcall EnumerateTraceGuids(ptr long ptr)
 # @ stub EnumerateTraceGuidsEx
 # @ stub EqualDomainSid
@@ -360,10 +360,10 @@
 @ stdcall GetSecurityInfo (long long long ptr ptr ptr ptr ptr)
 @ stdcall GetSecurityInfoExA (long long long str str ptr ptr ptr ptr)
 @ stdcall GetSecurityInfoExW (long long long wstr wstr ptr ptr ptr ptr)
-@ stdcall GetServiceDisplayNameA(ptr str ptr ptr)
-@ stdcall GetServiceDisplayNameW(ptr wstr ptr ptr)
-@ stdcall GetServiceKeyNameA(long str ptr ptr)
-@ stdcall GetServiceKeyNameW(long wstr ptr ptr)
+@ stdcall -import GetServiceDisplayNameA(ptr str ptr ptr)
+@ stdcall -import GetServiceDisplayNameW(ptr wstr ptr ptr)
+@ stdcall -import GetServiceKeyNameA(long str ptr ptr)
+@ stdcall -import GetServiceKeyNameW(long wstr ptr ptr)
 @ stdcall -import GetSidIdentifierAuthority(ptr)
 @ stdcall -import GetSidLengthRequired(long)
 @ stdcall -import GetSidSubAuthority(ptr long)
@@ -421,7 +421,7 @@
 @ stdcall -import IsValidSecurityDescriptor(ptr)
 @ stdcall -import IsValidSid(ptr)
 @ stdcall -import IsWellKnownSid(ptr long)
-@ stdcall LockServiceDatabase(ptr)
+@ stdcall -import LockServiceDatabase(ptr)
 @ stdcall LogonUserA(str str str long long ptr)
 # @ stub LogonUserExA
 # @ stub LogonUserExExW
@@ -526,7 +526,7 @@
 @ stdcall NotifyChangeEventLog (long long)
 # @ stub NotifyServiceStatusChange
 # @ stub NotifyServiceStatusChangeA
-@ stdcall NotifyServiceStatusChangeW(ptr long ptr)
+@ stdcall -import NotifyServiceStatusChangeW(ptr long ptr)
 # @ stub NpGetUserName
 @ stdcall ObjectCloseAuditAlarmA(str ptr long)
 @ stdcall -import ObjectCloseAuditAlarmW(wstr ptr long)
@@ -543,10 +543,10 @@
 @ stdcall OpenEventLogA (str str)
 @ stdcall OpenEventLogW (wstr wstr)
 @ stdcall -import OpenProcessToken(long long ptr)
-@ stdcall OpenSCManagerA(str str long)
-@ stdcall OpenSCManagerW(wstr wstr long)
-@ stdcall OpenServiceA(long str long)
-@ stdcall OpenServiceW(long wstr long)
+@ stdcall -import OpenSCManagerA(str str long)
+@ stdcall -import OpenSCManagerW(wstr wstr long)
+@ stdcall -import OpenServiceA(long str long)
+@ stdcall -import OpenServiceW(long wstr long)
 @ stdcall -import OpenThreadToken(long long long ptr)
 # @ stub OpenThreadWaitChainSession
 @ stdcall -ret64 OpenTraceA(ptr)
@@ -593,16 +593,16 @@
 # @ stub QueryLocalUserServiceName
 # @ stub QueryRecoveryAgentsOnEncryptedFile
 # @ stub QuerySecurityAccessMask
-@ stdcall QueryServiceConfig2A(long long ptr long ptr)
-@ stdcall QueryServiceConfig2W(long long ptr long ptr)
-@ stdcall QueryServiceConfigA(long ptr long ptr)
-@ stdcall QueryServiceConfigW(long ptr long ptr)
+@ stdcall -import QueryServiceConfig2A(long long ptr long ptr)
+@ stdcall -import QueryServiceConfig2W(long long ptr long ptr)
+@ stdcall -import QueryServiceConfigA(long ptr long ptr)
+@ stdcall -import QueryServiceConfigW(long ptr long ptr)
 # @ stub QueryServiceDynamicInformation
-@ stdcall QueryServiceLockStatusA(long ptr long ptr)
-@ stdcall QueryServiceLockStatusW(long ptr long ptr)
-@ stdcall QueryServiceObjectSecurity(long long ptr long ptr)
-@ stdcall QueryServiceStatus(long ptr)
-@ stdcall QueryServiceStatusEx (long long ptr long ptr)
+@ stdcall -import QueryServiceLockStatusA(long ptr long ptr)
+@ stdcall -import QueryServiceLockStatusW(long ptr long ptr)
+@ stdcall -import QueryServiceObjectSecurity(long long ptr long ptr)
+@ stdcall -import QueryServiceStatus(long ptr)
+@ stdcall -import QueryServiceStatusEx (long long ptr long ptr)
 # @ stub QueryTraceA
 @ stdcall QueryTraceW(int64 wstr ptr)
 # @ stub QueryUserServiceName
@@ -697,10 +697,10 @@
 @ stdcall RegisterEventSourceA(str str)
 @ stdcall RegisterEventSourceW(wstr wstr)
 # @ stub RegisterIdleTask
-@ stdcall RegisterServiceCtrlHandlerA(str ptr)
-@ stdcall RegisterServiceCtrlHandlerExA(str ptr ptr)
-@ stdcall RegisterServiceCtrlHandlerExW(wstr ptr ptr)
-@ stdcall RegisterServiceCtrlHandlerW(wstr ptr)
+@ stdcall -import RegisterServiceCtrlHandlerA(str ptr)
+@ stdcall -import RegisterServiceCtrlHandlerExA(str ptr ptr)
+@ stdcall -import RegisterServiceCtrlHandlerExW(wstr ptr ptr)
+@ stdcall -import RegisterServiceCtrlHandlerW(wstr ptr)
 @ stdcall RegisterTraceGuidsA(ptr ptr ptr long ptr str str ptr) ntdll.EtwRegisterTraceGuidsA
 @ stdcall RegisterTraceGuidsW(ptr ptr ptr long ptr wstr wstr ptr) ntdll.EtwRegisterTraceGuidsW
 @ stdcall RegisterWaitChainCOMCallback(ptr ptr)
@@ -760,18 +760,18 @@
 @ stdcall SetSecurityInfo (long long long ptr ptr ptr ptr)
 # @ stub SetSecurityInfoExA
 # @ stub SetSecurityInfoExW
-@ stdcall SetServiceBits(long long long long)
-@ stdcall SetServiceObjectSecurity(long long ptr)
-@ stdcall SetServiceStatus(long ptr)
+@ stdcall -import SetServiceBits(long long long long)
+@ stdcall -import SetServiceObjectSecurity(long long ptr)
+@ stdcall -import SetServiceStatus(long ptr)
 @ stdcall -import SetThreadToken(ptr ptr)
 @ stdcall -import SetTokenInformation(long long ptr long)
 # @ stub SetTraceCallback
 # @ stub SetUserFileEncryptionKey
 # @ stub SetUserFileEncryptionKeyEx
-@ stdcall StartServiceA(long long ptr)
-@ stdcall StartServiceCtrlDispatcherA(ptr)
-@ stdcall StartServiceCtrlDispatcherW(ptr)
-@ stdcall StartServiceW(long long ptr)
+@ stdcall -import StartServiceA(long long ptr)
+@ stdcall -import StartServiceCtrlDispatcherA(ptr)
+@ stdcall -import StartServiceCtrlDispatcherW(ptr)
+@ stdcall -import StartServiceW(long long ptr)
 @ stdcall StartTraceA(ptr str ptr)
 @ stdcall StartTraceW(ptr wstr ptr)
 @ stdcall StopTraceA(int64 str ptr)
@@ -828,7 +828,7 @@
 # @ stub TrusteeAccessToObjectA
 # @ stub TrusteeAccessToObjectW
 # @ stub UninstallApplication
-@ stdcall UnlockServiceDatabase (ptr)
+@ stdcall -import UnlockServiceDatabase (ptr)
 # @ stub UnregisterIdleTask
 @ stdcall UnregisterTraceGuids(int64) ntdll.EtwUnregisterTraceGuids
 @ stub UpdateTraceA
diff --git a/dlls/sechost/Makefile.in b/dlls/sechost/Makefile.in
new file mode 100644
index 00000000000..e2d525ade8c
--- /dev/null
+++ b/dlls/sechost/Makefile.in
@@ -0,0 +1,8 @@
+EXTRADEFS = -D_SECHOST_
+MODULE    = sechost.dll
+IMPORTLIB = sechost
+IMPORTS   = kernelbase
+DELAYIMPORTS = rpcrt4
+
+C_SRCS	  = service.c
+IDL_SRCS  = svcctl.idl
diff --git a/dlls/sechost/sechost.spec b/dlls/sechost/sechost.spec
new file mode 100644
index 00000000000..985f638f8ff
--- /dev/null
+++ b/dlls/sechost/sechost.spec
@@ -0,0 +1,46 @@
+@ stdcall ChangeServiceConfig2A(long long ptr)
+@ stdcall ChangeServiceConfig2W(long long ptr)
+@ stdcall ChangeServiceConfigA(long long long long wstr str ptr str str str str)
+@ stdcall ChangeServiceConfigW(long long long long wstr wstr ptr wstr wstr wstr wstr)
+@ stdcall CloseServiceHandle(long)
+@ stdcall ControlService(long long ptr)
+@ stdcall CreateServiceA(long str str long long long long str str ptr str str str)
+@ stdcall CreateServiceW(long wstr wstr long long long long wstr wstr ptr wstr wstr wstr)
+@ stdcall DeleteService(long)
+@ stdcall EnumDependentServicesA(long long ptr long ptr ptr)
+@ stdcall EnumDependentServicesW(long long ptr long ptr ptr)
+@ stdcall EnumServicesStatusA (long long long ptr long ptr ptr ptr)
+@ stdcall EnumServicesStatusExA(long long long long ptr long ptr ptr ptr str)
+@ stdcall EnumServicesStatusExW(long long long long ptr long ptr ptr ptr wstr)
+@ stdcall EnumServicesStatusW (long long long ptr long ptr ptr ptr)
+@ stdcall GetServiceDisplayNameA(ptr str ptr ptr)
+@ stdcall GetServiceDisplayNameW(ptr wstr ptr ptr)
+@ stdcall GetServiceKeyNameA(long str ptr ptr)
+@ stdcall GetServiceKeyNameW(long wstr ptr ptr)
+@ stdcall LockServiceDatabase(ptr)
+@ stdcall NotifyServiceStatusChangeW(ptr long ptr)
+@ stdcall OpenSCManagerA(str str long)
+@ stdcall OpenSCManagerW(wstr wstr long)
+@ stdcall OpenServiceA(long str long)
+@ stdcall OpenServiceW(long wstr long)
+@ stdcall QueryServiceConfig2A(long long ptr long ptr)
+@ stdcall QueryServiceConfig2W(long long ptr long ptr)
+@ stdcall QueryServiceConfigA(long ptr long ptr)
+@ stdcall QueryServiceConfigW(long ptr long ptr)
+@ stdcall QueryServiceLockStatusA(long ptr long ptr)
+@ stdcall QueryServiceLockStatusW(long ptr long ptr)
+@ stdcall QueryServiceObjectSecurity(long long ptr long ptr)
+@ stdcall QueryServiceStatusEx (long long ptr long ptr)
+@ stdcall QueryServiceStatus(long ptr)
+@ stdcall RegisterServiceCtrlHandlerA(str ptr)
+@ stdcall RegisterServiceCtrlHandlerExA(str ptr ptr)
+@ stdcall RegisterServiceCtrlHandlerExW(wstr ptr ptr)
+@ stdcall RegisterServiceCtrlHandlerW(wstr ptr)
+@ stdcall SetServiceBits(long long long long)
+@ stdcall SetServiceObjectSecurity(long long ptr)
+@ stdcall SetServiceStatus(long ptr)
+@ stdcall StartServiceA(long long ptr)
+@ stdcall StartServiceCtrlDispatcherA(ptr)
+@ stdcall StartServiceCtrlDispatcherW(ptr)
+@ stdcall StartServiceW(long long ptr)
+@ stdcall UnlockServiceDatabase (ptr)
diff --git a/dlls/advapi32/service.c b/dlls/sechost/service.c
similarity index 97%
rename from dlls/advapi32/service.c
rename to dlls/sechost/service.c
index 3bf6fdfd47f..71eda08605a 100644
--- a/dlls/advapi32/service.c
+++ b/dlls/sechost/service.c
@@ -1,5 +1,5 @@
 /*
- * Win32 advapi functions
+ * Win32 sechost functions
  *
  * Copyright 1995 Sven Verdoolaege
  * Copyright 2005 Mike McCormack
@@ -44,11 +44,11 @@
 #include "lmserver.h"
 
 #include "svcctl.h"
-
-#include "advapi32_misc.h"
+#include "dbt.h"
 
 #include "wine/exception.h"
 #include "wine/list.h"
+#include "wine/heap.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(service);
 
@@ -113,6 +113,17 @@ static BOOL stop_service;
 
 extern HANDLE CDECL __wine_make_process_system(void);
 
+static inline WCHAR *strdupAW( const char *src )
+{
+    WCHAR *dst = NULL;
+    if (src)
+    {
+        DWORD len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
+        if ((dst = heap_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
+    }
+    return dst;
+}
+
 static inline LPWSTR SERV_dupmulti(LPCSTR str)
 {
     UINT len = 0, n = 0;
@@ -651,7 +662,7 @@ static BOOL service_run_main_thread(void)
 }
 
 /******************************************************************************
- * StartServiceCtrlDispatcherA [ADVAPI32.@]
+ * StartServiceCtrlDispatcherA [SECHOST.@]
  *
  * See StartServiceCtrlDispatcherW.
  */
@@ -691,7 +702,7 @@ BOOL WINAPI StartServiceCtrlDispatcherA( const SERVICE_TABLE_ENTRYA *servent )
 }
 
 /******************************************************************************
- * StartServiceCtrlDispatcherW [ADVAPI32.@]
+ * StartServiceCtrlDispatcherW [SECHOST.@]
  *
  *  Connects a process containing one or more services to the service control
  * manager.
@@ -739,7 +750,7 @@ BOOL WINAPI StartServiceCtrlDispatcherW( const SERVICE_TABLE_ENTRYW *servent )
 }
 
 /******************************************************************************
- * LockServiceDatabase  [ADVAPI32.@]
+ * LockServiceDatabase  [SECHOST.@]
  */
 SC_LOCK WINAPI LockServiceDatabase (SC_HANDLE hSCManager)
 {
@@ -766,7 +777,7 @@ SC_LOCK WINAPI LockServiceDatabase (SC_HANDLE hSCManager)
 }
 
 /******************************************************************************
- * UnlockServiceDatabase  [ADVAPI32.@]
+ * UnlockServiceDatabase  [SECHOST.@]
  */
 BOOL WINAPI UnlockServiceDatabase (SC_LOCK ScLock)
 {
@@ -793,7 +804,7 @@ BOOL WINAPI UnlockServiceDatabase (SC_LOCK ScLock)
 }
 
 /******************************************************************************
- * SetServiceStatus [ADVAPI32.@]
+ * SetServiceStatus [SECHOST.@]
  *
  * PARAMS
  *   hService []
@@ -847,7 +858,7 @@ SetServiceStatus( SERVICE_STATUS_HANDLE hService, LPSERVICE_STATUS lpStatus )
 
 
 /******************************************************************************
- * OpenSCManagerA [ADVAPI32.@]
+ * OpenSCManagerA [SECHOST.@]
  *
  * Establish a connection to the service control manager and open its database.
  *
@@ -875,7 +886,7 @@ SC_HANDLE WINAPI OpenSCManagerA( LPCSTR lpMachineName, LPCSTR lpDatabaseName,
 }
 
 /******************************************************************************
- * OpenSCManagerW [ADVAPI32.@]
+ * OpenSCManagerW [SECHOST.@]
  *
  * See OpenSCManagerA.
  */
@@ -917,7 +928,7 @@ SC_HANDLE WINAPI OpenSCManagerW( LPCWSTR lpMachineName, LPCWSTR lpDatabaseName,
 }
 
 /******************************************************************************
- * ControlService [ADVAPI32.@]
+ * ControlService [SECHOST.@]
  *
  * Send a control code to a service.
  *
@@ -960,7 +971,7 @@ BOOL WINAPI ControlService( SC_HANDLE hService, DWORD dwControl,
 }
 
 /******************************************************************************
- * CloseServiceHandle [ADVAPI32.@]
+ * CloseServiceHandle [SECHOST.@]
  * 
  * Close a handle to a service or the service control manager database.
  *
@@ -998,7 +1009,7 @@ CloseServiceHandle( SC_HANDLE hSCObject )
 
 
 /******************************************************************************
- * OpenServiceA [ADVAPI32.@]
+ * OpenServiceA [SECHOST.@]
  *
  * Open a handle to a service.
  *
@@ -1027,7 +1038,7 @@ SC_HANDLE WINAPI OpenServiceA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
 
 
 /******************************************************************************
- * OpenServiceW [ADVAPI32.@]
+ * OpenServiceW [SECHOST.@]
  *
  * See OpenServiceA.
  */
@@ -1071,7 +1082,7 @@ SC_HANDLE WINAPI OpenServiceW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
 }
 
 /******************************************************************************
- * CreateServiceW [ADVAPI32.@]
+ * CreateServiceW [SECHOST.@]
  */
 SC_HANDLE WINAPI
 CreateServiceW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
@@ -1135,7 +1146,7 @@ CreateServiceW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
 
 
 /******************************************************************************
- * CreateServiceA [ADVAPI32.@]
+ * CreateServiceA [SECHOST.@]
  */
 SC_HANDLE WINAPI
 CreateServiceA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
@@ -1179,7 +1190,7 @@ CreateServiceA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
 
 
 /******************************************************************************
- * DeleteService [ADVAPI32.@]
+ * DeleteService [SECHOST.@]
  *
  * Delete a service from the service control manager database.
  *
@@ -1216,7 +1227,7 @@ BOOL WINAPI DeleteService( SC_HANDLE hService )
 
 
 /******************************************************************************
- * StartServiceA [ADVAPI32.@]
+ * StartServiceA [SECHOST.@]
  *
  * Start a service
  *
@@ -1267,7 +1278,7 @@ BOOL WINAPI StartServiceA( SC_HANDLE hService, DWORD dwNumServiceArgs,
 
 
 /******************************************************************************
- * StartServiceW [ADVAPI32.@]
+ * StartServiceW [SECHOST.@]
  * 
  * See StartServiceA.
  */
@@ -1297,7 +1308,7 @@ BOOL WINAPI StartServiceW(SC_HANDLE hService, DWORD dwNumServiceArgs,
 }
 
 /******************************************************************************
- * QueryServiceStatus [ADVAPI32.@]
+ * QueryServiceStatus [SECHOST.@]
  *
  * PARAMS
  *   hService        [I] Handle to service to get information about
@@ -1332,7 +1343,7 @@ BOOL WINAPI QueryServiceStatus(SC_HANDLE hService,
 
 
 /******************************************************************************
- * QueryServiceStatusEx [ADVAPI32.@]
+ * QueryServiceStatusEx [SECHOST.@]
  *
  * Get information about a service.
  *
@@ -1385,7 +1396,7 @@ BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel,
 }
 
 /******************************************************************************
- * QueryServiceConfigA [ADVAPI32.@]
+ * QueryServiceConfigA [SECHOST.@]
  */
 BOOL WINAPI QueryServiceConfigA( SC_HANDLE hService, LPQUERY_SERVICE_CONFIGA config,
                                  DWORD size, LPDWORD needed )
@@ -1475,7 +1486,7 @@ static DWORD size_string(LPCWSTR string)
 }
 
 /******************************************************************************
- * QueryServiceConfigW [ADVAPI32.@]
+ * QueryServiceConfigW [SECHOST.@]
  */
 BOOL WINAPI 
 QueryServiceConfigW( SC_HANDLE hService,
@@ -1549,7 +1560,7 @@ QueryServiceConfigW( SC_HANDLE hService,
 }
 
 /******************************************************************************
- * QueryServiceConfig2A [ADVAPI32.@]
+ * QueryServiceConfig2A [SECHOST.@]
  *
  * Note
  *   observed under win2k:
@@ -1605,7 +1616,7 @@ BOOL WINAPI QueryServiceConfig2A(SC_HANDLE hService, DWORD dwLevel, LPBYTE buffe
 }
 
 /******************************************************************************
- * QueryServiceConfig2W [ADVAPI32.@]
+ * QueryServiceConfig2W [SECHOST.@]
  *
  * See QueryServiceConfig2A.
  */
@@ -1713,7 +1724,7 @@ BOOL WINAPI QueryServiceConfig2W(SC_HANDLE hService, DWORD dwLevel, LPBYTE buffe
 }
 
 /******************************************************************************
- * EnumServicesStatusA [ADVAPI32.@]
+ * EnumServicesStatusA [SECHOST.@]
  */
 BOOL WINAPI
 EnumServicesStatusA( SC_HANDLE hmngr, DWORD type, DWORD state, LPENUM_SERVICE_STATUSA
@@ -1780,7 +1791,7 @@ EnumServicesStatusA( SC_HANDLE hmngr, DWORD type, DWORD state, LPENUM_SERVICE_ST
 }
 
 /******************************************************************************
- * EnumServicesStatusW [ADVAPI32.@]
+ * EnumServicesStatusW [SECHOST.@]
  */
 BOOL WINAPI
 EnumServicesStatusW( SC_HANDLE hmngr, DWORD type, DWORD state, LPENUM_SERVICE_STATUSW
@@ -1890,7 +1901,7 @@ EnumServicesStatusW( SC_HANDLE hmngr, DWORD type, DWORD state, LPENUM_SERVICE_ST
 }
 
 /******************************************************************************
- * EnumServicesStatusExA [ADVAPI32.@]
+ * EnumServicesStatusExA [SECHOST.@]
  */
 BOOL WINAPI
 EnumServicesStatusExA( SC_HANDLE hmngr, SC_ENUM_TYPE level, DWORD type, DWORD state,
@@ -1961,7 +1972,7 @@ EnumServicesStatusExA( SC_HANDLE hmngr, SC_ENUM_TYPE level, DWORD type, DWORD st
 }
 
 /******************************************************************************
- * EnumServicesStatusExW [ADVAPI32.@]
+ * EnumServicesStatusExW [SECHOST.@]
  */
 BOOL WINAPI
 EnumServicesStatusExW( SC_HANDLE hmngr, SC_ENUM_TYPE level, DWORD type, DWORD state,
@@ -2078,7 +2089,7 @@ EnumServicesStatusExW( SC_HANDLE hmngr, SC_ENUM_TYPE level, DWORD type, DWORD st
 }
 
 /******************************************************************************
- * GetServiceKeyNameA [ADVAPI32.@]
+ * GetServiceKeyNameA [SECHOST.@]
  */
 BOOL WINAPI GetServiceKeyNameA( SC_HANDLE hSCManager, LPCSTR lpDisplayName,
                                 LPSTR lpServiceName, LPDWORD lpcchBuffer )
@@ -2124,7 +2135,7 @@ BOOL WINAPI GetServiceKeyNameA( SC_HANDLE hSCManager, LPCSTR lpDisplayName,
 }
 
 /******************************************************************************
- * GetServiceKeyNameW [ADVAPI32.@]
+ * GetServiceKeyNameW [SECHOST.@]
  */
 BOOL WINAPI GetServiceKeyNameW( SC_HANDLE hSCManager, LPCWSTR lpDisplayName,
                                 LPWSTR lpServiceName, LPDWORD lpcchBuffer )
@@ -2177,7 +2188,7 @@ BOOL WINAPI GetServiceKeyNameW( SC_HANDLE hSCManager, LPCWSTR lpDisplayName,
 }
 
 /******************************************************************************
- * QueryServiceLockStatusA [ADVAPI32.@]
+ * QueryServiceLockStatusA [SECHOST.@]
  */
 BOOL WINAPI QueryServiceLockStatusA( SC_HANDLE hSCManager,
                                      LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus,
@@ -2189,7 +2200,7 @@ BOOL WINAPI QueryServiceLockStatusA( SC_HANDLE hSCManager,
 }
 
 /******************************************************************************
- * QueryServiceLockStatusW [ADVAPI32.@]
+ * QueryServiceLockStatusW [SECHOST.@]
  */
 BOOL WINAPI QueryServiceLockStatusW( SC_HANDLE hSCManager,
                                      LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus,
@@ -2201,7 +2212,7 @@ BOOL WINAPI QueryServiceLockStatusW( SC_HANDLE hSCManager,
 }
 
 /******************************************************************************
- * GetServiceDisplayNameA  [ADVAPI32.@]
+ * GetServiceDisplayNameA  [SECHOST.@]
  */
 BOOL WINAPI GetServiceDisplayNameA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
   LPSTR lpDisplayName, LPDWORD lpcchBuffer)
@@ -2248,7 +2259,7 @@ BOOL WINAPI GetServiceDisplayNameA( SC_HANDLE hSCManager, LPCSTR lpServiceName,
 }
 
 /******************************************************************************
- * GetServiceDisplayNameW  [ADVAPI32.@]
+ * GetServiceDisplayNameW  [SECHOST.@]
  */
 BOOL WINAPI GetServiceDisplayNameW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
   LPWSTR lpDisplayName, LPDWORD lpcchBuffer)
@@ -2301,7 +2312,7 @@ BOOL WINAPI GetServiceDisplayNameW( SC_HANDLE hSCManager, LPCWSTR lpServiceName,
 }
 
 /******************************************************************************
- * ChangeServiceConfigW  [ADVAPI32.@]
+ * ChangeServiceConfigW  [SECHOST.@]
  */
 BOOL WINAPI ChangeServiceConfigW( SC_HANDLE hService, DWORD dwServiceType,
   DWORD dwStartType, DWORD dwErrorControl, LPCWSTR lpBinaryPathName,
@@ -2339,7 +2350,7 @@ BOOL WINAPI ChangeServiceConfigW( SC_HANDLE hService, DWORD dwServiceType,
 }
 
 /******************************************************************************
- * ChangeServiceConfigA  [ADVAPI32.@]
+ * ChangeServiceConfigA  [SECHOST.@]
  */
 BOOL WINAPI ChangeServiceConfigA( SC_HANDLE hService, DWORD dwServiceType,
   DWORD dwStartType, DWORD dwErrorControl, LPCSTR lpBinaryPathName,
@@ -2379,7 +2390,7 @@ BOOL WINAPI ChangeServiceConfigA( SC_HANDLE hService, DWORD dwServiceType,
 }
 
 /******************************************************************************
- * ChangeServiceConfig2A  [ADVAPI32.@]
+ * ChangeServiceConfig2A  [SECHOST.@]
  */
 BOOL WINAPI ChangeServiceConfig2A( SC_HANDLE hService, DWORD dwInfoLevel, 
     LPVOID lpInfo)
@@ -2426,7 +2437,7 @@ BOOL WINAPI ChangeServiceConfig2A( SC_HANDLE hService, DWORD dwInfoLevel,
 }
 
 /******************************************************************************
- * ChangeServiceConfig2W  [ADVAPI32.@]
+ * ChangeServiceConfig2W  [SECHOST.@]
  */
 BOOL WINAPI ChangeServiceConfig2W( SC_HANDLE hService, DWORD dwInfoLevel, 
     LPVOID lpInfo)
@@ -2494,7 +2505,7 @@ static NTSTATUS SERV_QueryServiceObjectSecurity(SC_HANDLE hService,
 }
 
 /******************************************************************************
- * QueryServiceObjectSecurity [ADVAPI32.@]
+ * QueryServiceObjectSecurity [SECHOST.@]
  */
 BOOL WINAPI QueryServiceObjectSecurity(SC_HANDLE hService,
        SECURITY_INFORMATION dwSecurityInformation,
@@ -2512,7 +2523,7 @@ BOOL WINAPI QueryServiceObjectSecurity(SC_HANDLE hService,
 }
 
 /******************************************************************************
- * SetServiceObjectSecurity [ADVAPI32.@]
+ * SetServiceObjectSecurity [SECHOST.@]
  *
  * NOTES
  *  - SetSecurityInfo should be updated to call this function once it's implemented.
@@ -2526,7 +2537,7 @@ BOOL WINAPI SetServiceObjectSecurity(SC_HANDLE hService,
 }
 
 /******************************************************************************
- * SetServiceBits [ADVAPI32.@]
+ * SetServiceBits [SECHOST.@]
  */
 BOOL WINAPI SetServiceBits( SERVICE_STATUS_HANDLE hServiceStatus,
         DWORD dwServiceBits,
@@ -2548,7 +2559,7 @@ static DWORD WINAPI ctrl_handler_thunk( DWORD control, DWORD type, void *data, v
 }
 
 /******************************************************************************
- * RegisterServiceCtrlHandlerA [ADVAPI32.@]
+ * RegisterServiceCtrlHandlerA [SECHOST.@]
  */
 SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerA( LPCSTR name, LPHANDLER_FUNCTION handler )
 {
@@ -2556,7 +2567,7 @@ SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerA( LPCSTR name, LPHANDLER
 }
 
 /******************************************************************************
- * RegisterServiceCtrlHandlerW [ADVAPI32.@]
+ * RegisterServiceCtrlHandlerW [SECHOST.@]
  */
 SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerW( LPCWSTR name, LPHANDLER_FUNCTION handler )
 {
@@ -2564,7 +2575,7 @@ SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerW( LPCWSTR name, LPHANDLE
 }
 
 /******************************************************************************
- * RegisterServiceCtrlHandlerExA [ADVAPI32.@]
+ * RegisterServiceCtrlHandlerExA [SECHOST.@]
  */
 SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExA( LPCSTR name, LPHANDLER_FUNCTION_EX handler, LPVOID context )
 {
@@ -2578,7 +2589,7 @@ SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExA( LPCSTR name, LPHANDL
 }
 
 /******************************************************************************
- * RegisterServiceCtrlHandlerExW [ADVAPI32.@]
+ * RegisterServiceCtrlHandlerExW [SECHOST.@]
  */
 SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW( LPCWSTR lpServiceName,
         LPHANDLER_FUNCTION_EX lpHandlerProc, LPVOID lpContext )
@@ -2602,7 +2613,7 @@ SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerExW( LPCWSTR lpServiceNam
 }
 
 /******************************************************************************
- * EnumDependentServicesA [ADVAPI32.@]
+ * EnumDependentServicesA [SECHOST.@]
  */
 BOOL WINAPI EnumDependentServicesA( SC_HANDLE hService, DWORD dwServiceState,
                                     LPENUM_SERVICE_STATUSA lpServices, DWORD cbBufSize,
@@ -2616,7 +2627,7 @@ BOOL WINAPI EnumDependentServicesA( SC_HANDLE hService, DWORD dwServiceState,
 }
 
 /******************************************************************************
- * EnumDependentServicesW [ADVAPI32.@]
+ * EnumDependentServicesW [SECHOST.@]
  */
 BOOL WINAPI EnumDependentServicesW( SC_HANDLE hService, DWORD dwServiceState,
                                     LPENUM_SERVICE_STATUSW lpServices, DWORD cbBufSize,
@@ -2693,7 +2704,7 @@ static DWORD WINAPI notify_thread(void *user)
 }
 
 /******************************************************************************
- * NotifyServiceStatusChangeW [ADVAPI32.@]
+ * NotifyServiceStatusChangeW [SECHOST.@]
  */
 DWORD WINAPI NotifyServiceStatusChangeW(SC_HANDLE hService, DWORD dwNotifyMask,
         SERVICE_NOTIFYW *pNotifyBuffer)
diff --git a/dlls/advapi32/svcctl.idl b/dlls/sechost/svcctl.idl
similarity index 100%
rename from dlls/advapi32/svcctl.idl
rename to dlls/sechost/svcctl.idl  
